<?php

/**
 * Created by PhpStorm.
 * User: qiliping
 * Date: 2016/12/7
 * Time: 上午10:52
 */
class User_model extends CI_Model
{


	function get_user_by_openid($open_id){
		$sql = "SELECT * FROM {$this->db->dbprefix}user where open_id = '{$open_id}' LIMIT 1";
		return $this->db->query($sql)->row_array();
	}

	/**
	* 注册会员
	**/
	public function reg_user($phone,$rand_str,$password,$register_ip,$open_id)
	{
		//事务开始
		$this->db->trans_start();

		//查询上级
		$parent1 = $this->share_user;
		$parent2 = 0;
		$parent3 = 0;
		if($parent1>0){
			$parent1_sql = "SELECT id,parent1,parent2 FROM {$this->db->dbprefix}user where id={$parent1} and school_id={$this->school_id} LIMIT 1";
			$parent1_user = $this->db->query($parent1_sql)->row_array();
			if($parent1_user){
				$parent2 = $parent1_user['parent1'];
				$parent3 = $parent1_user['parent2'];
			}
		}
		// print_r($parent1);exit;

		$user_data = array(
		'login_name' => '',
		'open_id' => $open_id,
		'rand_str' => $rand_str,
		'password' => $password,
		'mobile' => $phone,
		'is_reg_xmpp' => 0,
		'sex' => 0,
		'register_ip' => $register_ip,
		'parent1' => $parent1,
		'parent2' => $parent2,
		'parent3' => $parent3,
		'status' => 1,
		'add_time' => time(),
		'edit_time' => 0,
		);
		$this->db->insert('user', $user_data);
		$user_id = $this->db->insert_id();

		$this->db->trans_complete();
		return $user_id;
	}
	/**
	 * 更新openid
	 * @param  [type] $user_id [description]
	 * @param  [type] $open_id [description]
	 * @return [type]          [description]
	 */
	public function update_openid($user_id,$open_id)
	{
		$user = array('open_id' => $open_id );
		$this->db->update('user', $user,array('id' => $user_id ));
	}
	/**
	* 根据用户id获取user_account
	*/
	public function get_user_account_by_id($user_id)
	{
		$sql = "SELECT * FROM {$this->db->dbprefix}user_account where user_id = '{$user_id}' LIMIT 1";
		return $this->db->query($sql)->row_array();
	}

	/**
	 * 获取我的预期收入
	 * @param [type] $user_id [description]
	 */
	function get_expect_income_by_id($user_id){
		$sql = "select sum(commission) as income from {$this->db->dbprefix}user_commission where user_id = $user_id";
		$result = $this->db->query($sql)->row_array();
		if($result){
			if($result['income']){
				return $result['income'];
			}else{
				return 0;
			}

		}else{
			return 0;
		}
	}

	/**
	 * 获取我的奖励
	 * @param [type] $user_id [description]
	 */
	function get_commission_by_user($user_id){
		$sql = "select * from {$this->db->dbprefix}user_commission where user_id = $user_id";
		return $this->db->query($sql)->result_array();
	}

	/**
	* 根据手机号获取用户信息
	*/
	function get_user_by_phone($phone){
		$sql = "SELECT * FROM {$this->db->dbprefix}user where mobile = '{$phone}' LIMIT 1";
		return $this->db->query($sql)->row_array();
	}

	/**
	* 根据主键id获取用户信息
	*/
	function get_user_by_id($user_id){
		$sql = "SELECT * FROM {$this->db->dbprefix}user where id = '{$user_id}' LIMIT 1";
		return $this->db->query($sql)->row_array();
	}

	function get_ask_user($limit = 4){
		$sql = "SELECT id, nick, head_image_url FROM {$this->db->dbprefix}user ORDER BY login_time DESC LIMIT $limit";
		return $this->db->query($sql)->result_array();
	}



	function get_school(){
		$url_wap = 'http://'.$_SERVER['HTTP_HOST'];
		$sql = "SELECT id,start_date,end_date,school_address,link_tel,link_phone,email,school_logo,url_wap,wx_share_logo,wx_share_text,school_name from {$this->db->dbprefix}o2o_school where url_wap = '".$url_wap."'";
		$result = $this->db->query($sql)->row_array();
		return $result;
	}

	/*function get_student_checkin_status($student_id, $class_id, $school_id, $plan_id){
		$time = time();

		$sql = "SELECT checkin.* FROM {$this->db->dbprefix}o2o_student_checkin AS checkin ";
		$sql .= "LEFT JOIN {$this->db->dbprefix}o2o_class_plan AS cp ON(cp.id=checkin.plan_id AND cp.school_id = checkin.school_id AND cp.class_id=checkin.plan_id) ";
		$sql .= "WHERE checkin.student_id = '{$student_id}' AND checkin.school_id = '{$school_id}' AND checkin.plan_id = '{$plan_id}' AND checkin.class_id = '{$class_id}' AND checkin.checkin_time ";
		return $this->db->query($sql)->row_array();
	}*/

	function get_student_checkin_status($school_id, $class_id, $student_id, $user_id, $plan_id, $plan_last_checkin_time){

		$sql = "SELECT * FROM {$this->db->dbprefix}o2o_student_checkin ";
		$sql .= "WHERE school_id = '{$school_id}' AND class_id = '{$class_id}' AND student_id = '{$student_id}' AND user_id = '{$user_id}' AND plan_id = '{$plan_id}' AND checkin_time <= '{$plan_last_checkin_time}' ";
		return $this->db->query($sql)->row_array();
	}

	function get_school_plan_class_time($plan_id){
		$sql = "SELECT class_date, class_time, class_time_str FROM {$this->db->dbprefix}o2o_class_plan";
	}

	/**
	 * 获取一级同学
	 * @param  [type] $parent [description]
	 * @return [type]         [description]
	 */
	function get_user_by_parent1($parent,$limit =0,$offset =10){
		$this->db->trans_start();

		$sql = "SELECT SQL_CALC_FOUND_ROWS u.id,u.login_name,u.nick,u.mobile,u.sex,u.add_time,u.is_vip,
			(select count(0) from {$this->db->dbprefix}user where parent1 = u.id and school_id = {$this->school_id}) as parent1_count,
			(select count(0) from {$this->db->dbprefix}user where parent2 = u.id and school_id = {$this->school_id}) as parent2_count,
			(select count(0) from {$this->db->dbprefix}user where parent3 = u.id and school_id = {$this->school_id}) as parent3_count
		 FROM {$this->db->dbprefix}user u where parent1 = {$parent} and school_id = {$this->school_id} LIMIT $limit, $offset;";

		$result = $this->db->query($sql)->result_array();
		$row_num =  $this->db->query('SELECT FOUND_ROWS() as row_num;')->row_array();
		$this->db->trans_complete();

		return array("total_count" => $row_num['row_num'], "result" => $result);
	}

	/**
	 * 获取二级同学
	 * @param  [type]  $parent [description]
	 * @param  integer $limit  [description]
	 * @param  integer $offset [description]
	 * @return [type]          [description]
	 */
	function get_user_by_parent2($parent,$limit =0,$offset =10){
		$this->db->trans_start();

		$sql = "SELECT SQL_CALC_FOUND_ROWS u.id,u.login_name,u.nick,u.mobile,u.sex,u.add_time,u.is_vip,
			(select count(0) from {$this->db->dbprefix}user where parent1 = u.id and school_id = {$this->school_id}) as parent1_count,
			(select count(0) from {$this->db->dbprefix}user where parent2 = u.id and school_id = {$this->school_id}) as parent2_count,
			(select count(0) from {$this->db->dbprefix}user where parent3 = u.id and school_id = {$this->school_id}) as parent3_count
		 FROM {$this->db->dbprefix}user u where parent2 = {$parent} and school_id = {$this->school_id} LIMIT $limit, $offset;";

		$result = $this->db->query($sql)->result_array();
		$row_num =  $this->db->query('SELECT FOUND_ROWS() as row_num;')->row_array();
		$this->db->trans_complete();

		return array("total_count" => $row_num['row_num'], "result" => $result);
	}

	/**
	 * 获取三级同学
	 * @param  [type]  $parent [description]
	 * @param  integer $limit  [description]
	 * @param  integer $offset [description]
	 * @return [type]          [description]
	 */
	function get_user_by_parent3($parent,$limit =0,$offset =10){
		$this->db->trans_start();

		$sql = "SELECT SQL_CALC_FOUND_ROWS u.id,u.login_name,u.nick,u.mobile,u.sex,u.add_time,u.is_vip,
			(select count(0) from {$this->db->dbprefix}user where parent1 = u.id and school_id = {$this->school_id}) as parent1_count,
			(select count(0) from {$this->db->dbprefix}user where parent2 = u.id and school_id = {$this->school_id}) as parent2_count,
			(select count(0) from {$this->db->dbprefix}user where parent3 = u.id and school_id = {$this->school_id}) as parent3_count
		 FROM {$this->db->dbprefix}user u where parent3 = {$parent} and school_id = {$this->school_id} LIMIT $limit, $offset;";

		$result = $this->db->query($sql)->result_array();
		$row_num =  $this->db->query('SELECT FOUND_ROWS() as row_num;')->row_array();
		$this->db->trans_complete();

		return array("total_count" => $row_num['row_num'], "result" => $result);
	}

	function get_user_commission($order_id){
		$sql = "SELECT * FROM {$this->db->dbprefix}user_commission WHERE order_id = '{$order_id}'";
		return $this->db->query($sql)->result_array();
	}
}
